/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class to create thread pool and run task on each thread
*  Name: LogPool.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.batch.core;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.pecats.batch.util.MoveFile;
import com.pecats.batch.util.ParseXMLFile;
import com.pecats.batch.data.ConfigData;
import com.pecats.batch.main.openXplore;
import com.pecats.batch.util.Cleanup;


/**
 * Used to create a pool of threads to run the batch module of OpenXplore
 */

public class LogPool implements Runnable 
{
 
	private final ExecutorService pool;
	File f1 = null;
	int dircounter;
	int filecounter;
	File[][] logfiles = new File[100][100];
	File[] dirfiles = null;
	private static LogPool logpool=null;

	public LogPool()
	{
		ParseXMLFile parseFile = new ParseXMLFile();
		parseFile.parseXML(openXplore.CONFIGDIR + openXplore.PATHSEPERATOR + openXplore.CONFIGFILE);
		pool = Executors.newFixedThreadPool(ConfigData.poolsize);		
	}

	public void run() 
	{ // run the service
	
		f1 = new File(ConfigData.rawlogsdir);
		System.out.println(ConfigData.rawlogsdir);		
		while(true)
			{			
				if (f1.isDirectory())
				{
					// Get list of Files
					dirfiles = f1.listFiles();				
				}

				// Cleanup Old Files
				pool.execute(new Cleanup());
								
				//Iterate directories and log files to Generate Reports for the customer
				for (dircounter=0; dircounter < dirfiles.length; dircounter++)
				{
					boolean processsuccess = (new File(ConfigData.processedlogsdir + openXplore.PATHSEPERATOR + dirfiles[dircounter].getName())).mkdir();
					boolean outputsuccess = (new File(ConfigData.outputdir + openXplore.PATHSEPERATOR + dirfiles[dircounter].getName())).mkdir();
					if (dirfiles[dircounter].isDirectory())
					{
						logfiles[dircounter] = dirfiles[dircounter].listFiles();
						for (filecounter=0; filecounter < logfiles[dircounter].length; filecounter++)
						{
							if (logfiles[dircounter][filecounter].getName().endsWith("log"))
							{
			        	 			pool.execute(new Handler(logfiles[dircounter][filecounter], dirfiles[dircounter]));
							}
						}
					}
     				}
				
				try
				{
					Thread.sleep(100000);
				}

				catch(InterruptedException ie)
				{
				}
		
/*				// Move processed files
				try
				{
					if (!pool.awaitTermination(ConfigData.pooltimeout, TimeUnit.SECONDS))
					{
						for (dircounter=0; dircounter < dirfiles.length; dircounter++)
						{
							if (dirfiles[dircounter].isDirectory())
							{
								System.out.println(ConfigData.processedlogsdir + openXplore.PATHSEPERATOR + dirfiles[dircounter].getName());
								for (filecounter=0; filecounter < logfiles[dircounter].length; filecounter++)
								{
									if (logfiles[dircounter][filecounter].getName().endsWith("log"))
									{
										MoveFile.move(logfiles[dircounter][filecounter], new File(ConfigData.processedlogsdir + openXplore.PATHSEPERATOR + dirfiles[dircounter].getName() + openXplore.PATHSEPERATOR  +logfiles[dircounter][filecounter].getName()));
			        		 				logfiles[dircounter][filecounter].delete();
									}
       							}
							}
						}
					}
				}
				catch(InterruptedException ie)
				{
				}
				catch (IOException io)
				{
				}
*/
			}
	}
 }